package com.neborosoft.android.gui.wrappers;

import android.app.Activity;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ViewFlipper;
import com.neborosoft.FlyingDog.android.MyActivity;
import com.neborosoft.android.gui.Gui;
import com.neborosoft.android.gui.adapters.listview.ViewArrayAdapter;
import com.nostra13.universalimageloader.core.assist.PauseOnScrollListener;

/**
 * Created with IntelliJ IDEA.
 * User: CM
 * Date: 02.12.12
 * Time: 14:46
 * To change this template use File | Settings | File Templates.
 */
public class ListViewSwitcher{
    private ViewFlipper switcher;

    public ListViewSwitcher(Activity activity, int viewFlipperId) {
        switcher = (ViewFlipper)activity.findViewById(viewFlipperId);
    }

    public AbsListView getListView(){
        int index = switcher.getDisplayedChild();
        return (AbsListView)switcher.getChildAt(index);
    }

    public void switchTo(int id){
        View listView = switcher.findViewById(id);
        id = switcher.indexOfChild(listView);
        switcher.setDisplayedChild(id);
    }

    public void setAdapter(ViewArrayAdapter adapter){
        AbsListView listView = getListView();
        ViewArrayAdapter oldAdapter = (ViewArrayAdapter)listView.getAdapter();
        if(adapter == oldAdapter){
            return;
        }

        if(oldAdapter != null){
            oldAdapter.onAdapterChanged();
        }

        Gui.setAdapter(listView, adapter);
    }

    public ViewArrayAdapter getAdapter(){
        return (ViewArrayAdapter)getListView().getAdapter();
    }

    public void switchTo(int id, ViewArrayAdapter adapter){
        switchTo(id);
        setAdapter(adapter);
    }
}
